<template>
  <div class="comprehensive-table-container auto-height-container">
    <obj-opbar
      :config_table="config_table"
      :is_recycle="is_recycle"
      @add="add"
      @com_del="com_del"
      @del="del"
      @fetch-data="selectData"
      @get-data="getData"
      @recovery="recovery"
      @recycle="recycle"
    />

    <obj-table
      ref="ObjTableRef"
      :config_table="config_table"
      :detail_column="detail_column"
      :detail_path="detail_path"
      :is_recycle="is_recycle"
      :list="list"
      :list-loading="listLoading"
      @edit="edit"
      @get-data="getData"
    />

    <vab-pagination
      :current-page="queryForm.page"
      :page-size="queryForm.page_num"
      :total="total"
      @current-change="currentChange"
      @size-change="pageChange"
    />
    <obj-edit ref="editRef" @fetch-data="getData" />
  </div>
</template>

<script lang="ts" setup>
import { api_datalist, api_recycle } from '/@/api/obj'

//设置表名
const tablename = 'account'
defineOptions({
  name: tablename,
})

// const router = useRouter()
//通用变量
const editRef = ref<any>(null)
const ObjTableRef = ref<any>(null)
const list = ref<any>([])
const listLoading = ref<boolean>(true)
const total = ref<any>(0)

//设置详情
const detail_path = '/system/obj_detail'
const detail_column = 2

const queryForm = reactive<any>({
  page: 1,
  page_num: 10,
})
const config_table = ref<any>({})
const is_recycle = ref<boolean>(false) //当前是否是回收站

//自定义变量

//自定义方法

//通用获取数据
const getData = async () => {
  listLoading.value = true
  api_datalist(tablename, queryForm).then((res: any) => {
    if (res) {
      is_recycle.value = false
      config_table.value = res.config_table
      list.value = res.datalist
      total.value = res.num
      listLoading.value = false
    }
  })
}
//通用进入回收站
const recycle = async () => {
  listLoading.value = true
  api_recycle(tablename, queryForm).then((res: any) => {
    if (res) {
      is_recycle.value = true
      config_table.value = res.config_table
      list.value = res.datalist
      total.value = res.num
      listLoading.value = false
    }
  })
}
//通用查询
const selectData = (data: any) => {
  Object.assign(queryForm, data)
  getData()
}
//通用普通删除(用于选择删除)
const del = (row: any) => {
  ObjTableRef.value.del(row)
}
//通用彻底删除
const com_del = (row: any) => {
  ObjTableRef.value.com_del(row)
}
//通用恢复操作
const recovery = (row: any) => {
  ObjTableRef.value.recovery(row)
}
//通用添加
const add = () => {
  editRef.value.showEdit(config_table.value)
}
//通用编辑
const edit = (row = {}) => {
  editRef.value.showEdit(row)
}
//通用每页数量修改
const pageChange = (value: number) => {
  queryForm.page = 1
  queryForm.page_num = value
  getData()
}
//通用翻页
const currentChange = (value: number) => {
  queryForm.page = value
  getData()
}

//通用加载前
onBeforeMount(() => {
  getData()
})
</script>
